﻿using System;
using System.Collections.Generic;
using System.Linq;
using TuxedoLib;

namespace SpaceSquirrel.SpriteRenderers
{
	class PlayerOverworldRenderer : SpriteRenderer
	{
		public override void Render(Sprite sprite, PlayScene scene, Image screen, int offsetX, int offsetY, int renderCounter)
		{
			int left = sprite.x + offsetX - 8;
			int top = sprite.y + offsetY - 8;
			bool gun = scene.lazorCooldown > 0;
			if (scene.side)
			{
				top -= 16;
			}

			Image img;
			if (sprite.bikeMode)
			{
				if ((renderCounter & 1) == 0)
				{
					img = ImageLibrary.GetImage("sprites/rocket_bike_1.png");
				}
				else
				{
					img = ImageLibrary.GetImage("sprites/rocket_bike_2.png");
				}
			}
			else if (scene.side)
			{
				string dir;
				if (sprite.damageDir != null)
				{
					dir = sprite.damageDir;
				}
				else
				{
					dir = sprite.lastDirection;
				}
				bool reverse = dir == "left";

				string baze = scene.hasAtmosphere ? "cave" : "space";
				bool moving = sprite.moving;
				string path;
				if (sprite.cling)
				{
					path = baze + "_climb_";
					if (gun)
					{
						path += "shoot";
						if (reverse)
						{
							left -= 8;
						}
						else
						{
							left += 8;
						}
					}
					else if (moving)
					{
						path += "1232"[renderCounter % 4];
					}
					else
					{
						path += "2";
					}
				}
				else
				{
					path = baze + "_";
					if (gun)
					{
						if (moving)
						{
							path += "shooting_" + "1213"[renderCounter % 4];
						}
						else
						{
							path += "shooting";
						}
					}
					else
					{
						if (moving || (sprite.cling && sprite.vy != 0))
						{
							// TODO: running, cut RC in half
							path += "1213"[renderCounter % 4];
						}
						else
						{
							path += "1";
						}
					}
				}

				int x = left;
				int y = top;
				if (sprite.deathState != null && sprite.deathState != "fall")
				{
					if (sprite.deathState == "collapse")
					{
						img = ImageLibrary.GetImage("sprites/" + baze + "_death.png");
						top += 16;
						left -= 8;
					}
					else if (sprite.deathState == "lava")
					{
						if (reverse)
						{
							img = ImageLibrary.GetBackwardsImage("sprites/roasted.png");
						}
						else
						{
							img = ImageLibrary.GetImage("sprites/roasted.png");
							left = left + 16 - img.Width;
						}
					}
					else
					{
						throw new InvalidOperationException("this should not happen");
					}
				}
				else
				{
					if (sprite.sprinkle)
					{
						img = ImageLibrary.GetImage("sprites/space_sprinkle.png");
						left = left + 16 - img.Width;
					}
					else if (reverse)
					{
						img = ImageLibrary.GetBackwardsImage("sprites/" + path + ".png");
					}
					else
					{
						img = ImageLibrary.GetImage("sprites/" + path + ".png");
						left = left + 16 - img.Width;
					}
				}
			}
			else
			{
				string counter = sprite.moving ? ("" + "1232"[renderCounter & 3]) : "2";
				string dir = sprite.lastDirection;
				bool reverse = false;
				if (dir == "left")
				{
					reverse = true;
					dir = "right";
				}
				string baze = "space";
				Context ctx = scene.Context;
				if (ctx.VolcanoA && ctx.VolcanoB && ctx.VolcanoC)
				{
					baze = "cave";
				}
				string path = "sprites/" + baze + "_overworld_" + dir + "_" + counter + ".png";
				img = reverse ? ImageLibrary.GetBackwardsImage(path) : ImageLibrary.GetImage(path);
			}
			screen.Blit(img, left, top);
		}
	}
}
